import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/http/http.dart';


class TVcard extends StatefulWidget {
  const TVcard({Key key}) : super(key: key);

  @override
  _TVcardState createState() => _TVcardState();
}

class _TVcardState extends State<TVcard> with SingleTickerProviderStateMixin {
  int state = 1;
  String img1 = 'assets/images/common/tv-on.png';
  String img2 = 'assets/images/common/tv-off.png';
  Animation<double> animation;
  AnimationController _controller;
  @override
  void dispose() {
    _controller.stop();
    _controller.dispose();
    super.dispose();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getishow();
    _controller = new AnimationController(
        duration: const Duration(milliseconds: 1000), vsync: this);
    animation = new Tween(begin: 130.0, end: 145.0).animate(_controller)
      ..addListener(() {
        setState(() {
          // the state that has changed here is the animation object’s value
        });
      });
    animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        _controller.forward();
      }
    });
    _controller.forward();
  }

  void _getishow() async {
    var response = await Http.get('/television/getById?id=10000');
    print(response);
    if (response['content'] == 'isopen')
      state = 1;
    else
      state = 0;
  }

  _turnon() async {
    var response = await Http.post(
        '/devicestate/operation?typeId=5&deviceId=10000&operation=turnon');
    print(response);
  }

  _turnoff() async {
    var response = await Http.post(
        '/devicestate/operation?typeId=5&deviceId=10000&operation=turnoff');
    print(response);
  }

  @override
  Widget build(BuildContext context) {
    String img3 = state == 1 ? img1 : img2;
    return Material(
      color: Color.fromRGBO(255, 250, 212, 1),
      child: Stack(
        alignment: Alignment.center,
        children: [
          Positioned(
              top: animation.value, child: Image.asset('assets/images/common/dianshi.png')),
          Scaffold(
            backgroundColor: Colors.transparent,
            appBar: AppBar(
              elevation: 0,
              backgroundColor: Color.fromRGBO(255, 250, 212, 1),
              leading: InkWell(
                onTap: () {
                  Navigator.pop(context);
                },
                child: Icon(
                  Icons.keyboard_arrow_left,
                  color: Color.fromRGBO(72, 72, 72, 1),
                ),
              ),
            ),
            body: Container(
              // width: 360,
              height: 640,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  Positioned(
                      top: 335,
                      child: Container(
                          width: 280,
                          height: 200,
                          decoration: new BoxDecoration(
                            boxShadow: [
                              BoxShadow(
                                  color: Color.fromRGBO(10, 10, 10, 0.2),
                                  offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                                  blurRadius: 20.0, //阴影模糊程度
                                  spreadRadius: 0.5 //阴影扩散程度
                                  )
                            ],
                            gradient: LinearGradient(
                              begin: Alignment.topLeft,
                              end: Alignment.bottomRight,
                              colors: [
                                Color.fromRGBO(255, 255, 255, 1),
                                Color.fromRGBO(255, 255, 255, 1),
                              ],
                            ),
                            borderRadius:
                                BorderRadius.all(Radius.circular(20.0)),
                          ),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: [
                              Text(
                                '工作状态',
                                style: TextStyle(
                                    fontSize: 26,
                                    color: Color.fromRGBO(72, 72, 72, 1),
                                    fontWeight: FontWeight.bold),
                              ),
                              Image.asset(img3),
                              CupertinoSwitch(
                                trackColor: Color.fromRGBO(255, 250, 212, 1),
                                activeColor: Colors.blueGrey[600],
                                value: this.state == 1,
                                onChanged: (bool value) {
                                  setState(() {
                                    // this.state = value;
                                    if (value) {
                                      _turnon();
                                      this.state = 1;
                                    } else {
                                      _turnoff();
                                      this.state = 0;
                                    }
                                  });
                                },
                              )
                            ],
                          )))
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}
